package com.bana.fvs.app.main.adapter;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.common.ConstantsField;
import com.bana.fvs.app.main.entity.FogZoneListItemBean;
import com.wei.commonlibrary.utils.GlobalContext;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    public List<FogZoneListItemBean> datas;
    private onItemClickListener mOnItemClickListener;


    public MyAdapter(List<FogZoneListItemBean> datas) {
        this.datas = datas;
    }

    public interface onItemClickListener {
        void onClickListener(int position);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout_fog_list, viewGroup, false);
        return new ViewHolder(view);
    }

    private static final String TAG = "debug_MyAdapter";

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        FogZoneListItemBean bean = datas.get(position);
        if (null == bean) return;
        if (!TextUtils.isEmpty(bean.getWqmc())) {
            holder.tvFogListTitle.setText(bean.getWqmc());
        }
        //能见度
//        if (!TextUtils.isEmpty(bean.getNjd())) {
        String level = "";
        level = bean.getNjddj();
        holder.tvVisibleDistance.setText(String.format("等级%s级", level));
        if (level.equals("-1")) {
            holder.tvVisibleDistance.setVisibility(View.INVISIBLE);
//            holder.tvWeather.setText("天气:-");
        } else {
            holder.tvVisibleDistance.setVisibility(View.VISIBLE);

//            holder.tvWeather.setText("天气：晴朗");
        }
        if (bean.getLmzt() != null) {
//            holder.tvWeather.setText("天气：" + bean.getLmzt());
            holder.tvWeather.setText("天气：" + bean.getTqdmsm());
        }else{
            holder.tvWeather.setText("天气:-");
        }
//        }
        //天气
        //holder.tvWeather.setText(String.format("路面:%s", bean.getLmzt()));


        //能见度
        Drawable icon = null;
        if (bean.getJcysbzt() != 1) {
            icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_5);
        } else {
            switch (bean.getNjddj()) {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_1);
                    break;
                case "6":
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_2);
                    break;
                case "7":
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_3);
                    break;
                case "8":
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_4);
                    break;
                case "9":
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_5);
                    break;
                default:
                    icon = GlobalContext.context.getResources().getDrawable(R.drawable.fog_level_1);
                    break;

            }
        }
        level = bean.getNjddj();
        if (bean.getNjd() != 0) {
            if(bean.getNjd()> ConstantsField.MAXNJD) {
                holder.tvVisibleLevel.setText(String.format("能见度:%sm", bean.getNjd()));
            }else{
                holder.tvVisibleLevel.setText("能见度:-");
            }
        }
        else
            holder.tvVisibleLevel.setText(bean.getJcysbztsm());
        //holder.tvVisibleLevel.setText(String.format("能见度等级:%s级", level));
        holder.tvVisibleLevel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        holder.ll_rv_container.setOnClickListener(v -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onClickListener(position);
            }
        });
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //标题
        public TextView tvFogListTitle;
        //可见度
        public TextView tvVisibleLevel;
        //可见距离
        public TextView tvVisibleDistance;
        //天气状况
        public TextView tvWeather;
        public LinearLayout ll_rv_container;

        public ViewHolder(View view) {
            super(view);
            tvFogListTitle = (TextView) view.findViewById(R.id.tvFogListTitle);
            tvVisibleLevel = (TextView) view.findViewById(R.id.tvVisibleLevel);
            tvVisibleDistance = (TextView) view.findViewById(R.id.tvVisibleDistance);
            tvWeather = (TextView) view.findViewById(R.id.tvWeather);
            ll_rv_container = (LinearLayout) view.findViewById(R.id.ll_rv_container);
        }
    }
}
